//
//  ST_NotificationAuthorityController.swift
//  BTVideo
//
//  Created by DouLei on 2019/10/21.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyUserDefaults
import SwifterSwift
import UserNotifications

class ST_NotificationAuthorityController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.white
        
        view.addSubview(welcome_view)
        welcome_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(33)
            make.right.equalToSuperview().offset(-33)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(welcome_view.snp.width)
        }
        welcome_view.play()
        
        
        view.addSubview(content_label1)
        content_label1.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(welcome_view.snp.bottom).offset(-45)
        }
        
        view.addSubview(content_label2)
        content_label2.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label1.snp.bottom).offset(10)
        }
        
        view.addSubview(tips_btn1)
        tips_btn1.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(100.0/375.0*ScreenWidth)
            make.top.equalTo(content_label2.snp.bottom).offset(40)
        }
        
        view.addSubview(tips_btn2)
        tips_btn2.snp.makeConstraints { (make) in
            make.left.equalTo(tips_btn1)
            make.top.equalTo(tips_btn1.snp.bottom).offset(15)
        }
        
        view.addSubview(tips_btn3)
        tips_btn3.snp.makeConstraints { (make) in
            make.left.equalTo(tips_btn1)
            make.top.equalTo(tips_btn2.snp.bottom).offset(15)
        }
        
        
        view.addSubview(request_btn)
        request_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(tips_btn3.snp.bottom).offset(48)
            make.width.equalTo(220)
            make.height.equalTo(40)
        }
    }
    

    lazy var welcome_view:LOTAnimationView = {
        
        let path = Bundle.main.path(forResource: "requestNotificationAuthority", ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFit
        
        control.loopAnimation = true
        
        return control
    }()

    lazy var content_label1:UILabel = {
        
        let control = UILabel.init()
        control.text = "开启通知"
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()
    
    lazy var content_label2:UILabel = {
        
        let control = UILabel.init()
        control.text = "你就可以收到："
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()

    
    
    lazy var tips_btn1:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("成为好友消息  ")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        control.setTitleColor(UIColor.init(hexString: "#666666"), for: UIControl.State.normal)
        control.setImageForAllStates(ST_ImageUtils.imageName("IndexItem_icon_emoji匹配成功")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 8, bottom: 0, right: 0)
        return control
    }()

    lazy var tips_btn2:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("点赞、评论、关注通知  ")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        control.setTitleColor(UIColor.init(hexString: "#666666"), for: UIControl.State.normal)
        control.setImageForAllStates(ST_ImageUtils.imageName("IndexItem_icon_emoji谁喜欢了你")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 8, bottom: 0, right: 0)
        return control
    }()
    
    lazy var tips_btn3:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("心仪对象给你发消息  ")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.regular)
        control.setTitleColor(UIColor.init(hexString: "#666666"), for: UIControl.State.normal)
        control.setImageForAllStates(ST_ImageUtils.imageName("IndexItem_icon_emoji心仪消息")!)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 8, bottom: 0, right: 0)
        return control
    }()
    
    lazy var request_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 20
//        control.borderColor = Color.init(white: 0, alpha: 0.1)
//        control.borderWidth = 1
        control.setTitleForAllStates("打开通知")
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        
        control.setTitleColorForAllStates(UIColor.white)
        
        control.addTarget(self, action: #selector(b_t_notification_permission), for: UIControl.Event.touchUpInside)
        
        return control
    }()

//    MARK: - Event
    @objc func b_t_notification_permission(){
        
        self.view.removeFromSuperview()
        
        var options :UNAuthorizationOptions = [.badge, .alert, .sound]
        
        if #available(iOS 12, *) {
            options = [.badge, .alert, .sound, .provisional, .providesAppNotificationSettings, .criticalAlert]
        }
        
        UNUserNotificationCenter.current().requestAuthorization(options:options){ (granted, error) in
          
                if granted{
                    
                    ST_BTGlobalDataCenter.shared.allow_notification_event()
                    
                }
            
        }
        
        UIApplication.shared.registerForRemoteNotifications()
        
        self.removeFromParent()
        
    }
    
    

}
